package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.WebSocket.TestWebSocketHandler;
import com.example.onlinemonitoring.dao.DlqjchistoryTestMapper;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Byqpdsensor;
import com.example.onlinemonitoring.model.Dlhlsensor;
import com.example.onlinemonitoring.model.Dljfsensor;
import com.example.onlinemonitoring.model.Dlqjchistory;
import com.example.onlinemonitoring.model.DlqjchistoryTest;
import com.example.onlinemonitoring.model.Dlqjcsensor;
import com.example.onlinemonitoring.model.Dlqjxtxsensor;
import com.example.onlinemonitoring.model.Gbjfsensor;
import com.example.onlinemonitoring.model.Gbzdsensor;
import com.example.onlinemonitoring.model.Gispdsensor;
import com.example.onlinemonitoring.model.Imsensor;
import com.example.onlinemonitoring.model.Newblqsensor;
import com.example.onlinemonitoring.model.Gisblqsensor;
import com.example.onlinemonitoring.model.Zxdblqsensor;
import com.example.onlinemonitoring.model.O2andsf6sensor;
import com.example.onlinemonitoring.model.Oilsensor;
import com.example.onlinemonitoring.model.Oilfxsensor;
import com.example.onlinemonitoring.model.Sctmnsensor;
import com.example.onlinemonitoring.model.Sctmsensor;
import com.example.onlinemonitoring.model.Sf6sensor;
import com.example.onlinemonitoring.model.Txpdsensor;
import com.example.onlinemonitoring.model.Yggxsensor;
import com.example.onlinemonitoring.model.Tgsensor;
import com.example.onlinemonitoring.model.Arcsupcoilsensor;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.serviceImpl.AppdeviceServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ByqpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlhlsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DljfsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlqjcsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlqjxtxsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GbjfsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GbzdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GispdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ImsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.NewblqsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GisblqsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ZxdblqsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.O2andsf6sensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.OilsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.OilfxsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.SctmnsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.SctmsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.Sf6sensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.TxpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.YggxsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.TgsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ArcsupcoilsensorServiceImpl;

import tk.mybatis.mapper.entity.Example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Scope;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import javax.print.PrintException;

/**
 * 全站数据转发，转发给61850服务器用的
 */
@Service("dfCom")
@Scope("prototype")
public class DfCom extends CommPortManager {
    String currentdeviceid = "";
    HashMap<Integer, String> devmap = new HashMap<>();
    @Autowired
    private AppdeviceServiceImpl appdeviceserviceimpl;
    @Autowired
    private ByqpdsensorServiceImpl byqpdsensorserviceimpl;
    @Autowired
    private GispdsensorServiceImpl gispdsensorserviceimpl;
    @Autowired
    private OilsensorServiceImpl oilsensorserviceimpl;
    @Autowired
    private OilfxsensorServiceImpl oilfxsensorserviceimpl;
    @Autowired
    private SctmsensorServiceImpl sctmsensorserviceimpl;
    @Autowired
    private SctmnsensorServiceImpl sctmnsensorserviceimpl;
    @Autowired
    private ImsensorServiceImpl imsensorserviceimpl;
    @Autowired
    private YggxsensorServiceImpl yggxsensorserviceimpl;
    @Autowired
    private NewblqsensorServiceImpl newblqsensorserviceimpl;
    @Autowired
    private TxpdsensorServiceImpl txpdsensorserviceimpl;
    @Autowired
    private Sf6sensorServiceImpl sf6sensorserviceimpl;
    @Autowired
    private DlqjcsensorServiceImpl dlqserviceimpl;
    @Autowired
    private DlqjchistoryTestMapper dlqjchistorytestmapper;
    @Autowired
    private O2andsf6sensorServiceImpl o2andsf6sensorserviceimpl;
    @Autowired
    private GbzdsensorServiceImpl gbzdsensorserviceimpl;
    @Autowired
    private GbjfsensorServiceImpl gbjfsensorserviceimpl;
    @Autowired
    private DlhlsensorServiceImpl dlhlsensorserviceimpl;
    @Autowired
    private DljfsensorServiceImpl dljfsensorserviceimpl;
    @Autowired
    private TgsensorServiceImpl tgsensorserviceimpl;
    @Autowired
    private ArcsupcoilsensorServiceImpl arcsupcoilsensorServiceImpl;
    @Autowired
    private GisblqsensorServiceImpl gisblqsensorServiceImpl;
    @Autowired
    private ZxdblqsensorServiceImpl zxdblqsensorServiceImpl;
    @Autowired
    private DlqjxtxsensorServiceImpl dlqjxtxsensorserviceimpl;

    private HashMap<String, String> devgroupmap = new HashMap<>();
    List<String> devstr = new ArrayList<>();
    public String checkType = "crc16";
    public String[] dev = { "5209", "5DC1", "520A", "520C", "5210", "520E", "59D9", "520B", "5213", "5DC2", "5DC3",
            "5214",
            "5215", "5216", "5217", "5218", "5219", "5220", "5230", "5231" };
    public static String[] iec61850Cof = {
            "BYQJF", "GISJF", "YSP", "TX", "KGG", "ZB", "BLQ", "SF6", "", "DLQ", "", "HJJK", "", "", "", "", "TG", "",
            "ZXDBLQ", "YW"
    };

    @Override
    protected void myInit() {
        if (ad != null && ad.getSubswitch() != null && !ad.getSubswitch().equals("")) {
            String[] subswitch = ad.getSubswitch().split(",");// 1|1-4
            for (int i = 0; i < dev.length; i++) {
                if (i >= subswitch.length) {
                    break;
                }
                String[] sub = subswitch[i].split("\\|");
                if (sub[0].equals("1")) {
                    devstr.add(dev[i]);
                    devgroupmap.put(dev[i], sub[1]);// 1-4
                }
                // devidmap.put(Integer.parseInt(sub[0]), i);

            }
        }
    }

    @Override
    @Transactional
    public void receiveData(String str, OutputStream out) {
        try {
            str = str.toUpperCase();
            setText(str);
            switch (str.substring(2, 4)) {
                case "06": {// 0106000152126567
                    switch (str.substring(4, 8)) {
                        case "0001": {
                            if (str.substring(8, 12).equals("5DC3") && devstr.contains("5DC3")) {// 断路器谱图
                                Example example1 = new Example(DlqjchistoryTest.class);
                                Example.Criteria criteria1 = example1.createCriteria();
                                criteria1.andEqualTo("stationid", ad.getStationid());
                                criteria1.andEqualTo("devicegroup", ad.getDevicegroup());
                                int count = dlqjchistorytestmapper.selectCountByExample(example1);
                                if (count == 0) {
                                    String s = str.substring(0, 2) + "0600000000";
                                    s += CRC16.calcCrc16(s);
                                    Send(s, out);
                                    currentdeviceid = "";
                                } else {
                                    currentdeviceid = str.substring(8, 12);
                                    Send(str, out);
                                }
                            } else {
                                if (!devstr.contains(str.substring(8, 12))) {
                                    String s = str.substring(0, 2) + "0600000000";
                                    s += CRC16.calcCrc16(s);
                                    Send(s, out);
                                    currentdeviceid = "";
                                } else {
                                    currentdeviceid = str.substring(8, 12);
                                    Send(str, out);
                                }
                            }
                        }
                            break;
                        case "0009": {
                            Long id = (long) Integer.parseInt(str.substring(8, 16), 16);
                            DlqjchistoryTest currdlq = new DlqjchistoryTest();
                            currdlq.setId(id);
                            dlqjchistorytestmapper.deleteByPrimaryKey(currdlq);
                        }
                            break;
                        default:
                            break;
                    }

                }
                    break;
                case "03": {// 010300020007A5C8
                    if (!currentdeviceid.equals("")) {
                        resolveData(str, out);
                    }
                }
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void resolveData(String str, OutputStream out) {
        int groupstart = ad.getDevicegroup();
        int groupend = ad.getDevicegroup();
        try {
            groupstart = Integer.parseInt(devgroupmap.get(currentdeviceid).split("-")[0]);
            groupend = Integer.parseInt(devgroupmap.get(currentdeviceid).split("-")[1]);
        } catch (Exception e) {
            // TODO: handle exception
        }

        switch (currentdeviceid) {
            case "5209":// 变压器局放
                resolveByqpd(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5DC1":// GIS局放
                resolveGispd(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "520A":// 油色谱
                if (ad.getBrand().equals("DGA9"))
                    resolveOilDGA9(str, ad.getStationid(), groupstart, groupend, out);
                else if (ad.getBrand().equals("DGA3"))
                    resolveOilDGA3(str, ad.getStationid(), groupstart, groupend, out);
                else
                    resolveOil(str, ad.getStationid(), groupstart, groupend, out);

                break;
            case "520B":// 微水
                resolveSf6(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "520C":// 铁芯
                resolveTxpd(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5210":// 开关柜
                resolveSctmn(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "520E":// 荧光光纤
                resolveYggx(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "59D9":// 避雷器
                resolveBlq(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5212":// 变压器中性点直流装置（智能监控器）
                // resolveIm(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5213":// 铁芯（智能监控器）
                resolveTxpdIm(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5DC2":// 断路器常态数据
                resolveDlqNormal(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5DC3":// 断路器波形数据
                resolveDlq(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5214":// 环境监控
                resolveHjjk(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5215":// 干变振动
                resolveGbzd(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5216":// 干变局放
                resolveGbjf(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5217":// 电缆环流
                resolveDlhl(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5218":// 电缆局放
                resolveDljf(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5219":// 套管
                resolveTg(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5220":// 消弧线圈控制器
                resolveXh(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5230":// 中性点避雷器
                resolveZxdBlq(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5231":// 油温油位
                resolveYw(str, ad.getStationid(), groupstart, groupend, out);
                break;
            case "5DC4":// 断路器机械特性
                resolveDlqjxtx(str, ad.getStationid(), groupstart, groupend, out);
                break;
            default:
                break;
        }
    }

    private void resolveDlqNormal(String str, Integer stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Dlqjcsensor record = new Dlqjcsensor();
            record.setStationid(stationid);

            record.setCmd(67);
            List<Dlqjcsensor> list = dlqserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                Dlqjcsensor dlq = new Dlqjcsensor();
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, isnull(record.getLastastoragestate()) == 1 ? "0001" : "0000");
                devmap.put(index++, isnull(record.getLastastate()) == 1 ? "0001" : "0000");
                {
                    Example example = new Example(Dlqjcsensor.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("stationid", record.getStationid());
                    criteria.andEqualTo("devicegroup", record.getDevicegroup());
                    criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
                    List<Dlqjcsensor> innerlist = dlqserviceimpl.selectByExample(example);
                    boolean iskase = false, iskazhi = false, ispianli = false;
                    for (Dlqjcsensor innerdlq : innerlist) {
                        if (isnull(innerdlq.getLastalarmmessage()).contains("卡涩")) {
                            iskase = true;
                        }
                        if (isnull(innerdlq.getLastalarmmessage()).contains("卡滞")) {
                            iskazhi = true;
                        }
                        if (isnull(innerdlq.getLastalarmmessage()).contains("偏离")) {
                            ispianli = true;
                        }
                    }
                    devmap.put(index++, iskase ? "0001" : "0000");
                    devmap.put(index++, iskazhi ? "0001" : "0000");
                    devmap.put(index++, ispianli ? "0001" : "0000");
                }

                devmap.put(index++, String.format("%04x", (short) (isnull(record.getLastaacurrent()) * 10)));
                devmap.put(index++, String.format("%04x", (short) (isnull(record.getLastaacurrent()) * 10)));
                {
                    Example example = new Example(Dlqjcsensor.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("stationid", record.getStationid());
                    criteria.andEqualTo("devicegroup", record.getDevicegroup());
                    criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
                    criteria.andEqualTo("cmd", 64);
                    criteria.andEqualTo("laststate", 2);
                    dlq = dlqserviceimpl.selectByExample(example).get(0);
                    devmap.put(index++, String.format("%04x", isnull(dlq.getAcount())));
                }
                devmap.put(index++, String.format("%04x", (short) ((100 - isnull(record.getLifetime())) * 10)));
                {
                    Example example = new Example(Dlqjcsensor.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("stationid", record.getStationid());
                    criteria.andEqualTo("devicegroup", record.getDevicegroup());
                    criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
                    criteria.andEqualTo("cmd", 64);
                    criteria.andEqualTo("laststate", 2);
                    dlq = dlqserviceimpl.selectByExample(example).get(0);
                    devmap.put(index++, String.format("%04x", (short) (isnull(dlq.getLastspeed()) * 10)));
                }
                {
                    Example example = new Example(Dlqjcsensor.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("stationid", record.getStationid());
                    criteria.andEqualTo("devicegroup", record.getDevicegroup());
                    criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
                    criteria.andEqualTo("cmd", 64);
                    criteria.andEqualTo("laststate", 1);
                    dlq = dlqserviceimpl.selectByExample(example).get(0);
                    devmap.put(index++, String.format("%04x", (short) (isnull(dlq.getLastspeed()) * 10)));
                }
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "dlqjc");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "000F";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveDlq(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            DlqjchistoryTest record = new DlqjchistoryTest();
            Example example1 = new Example(DlqjchistoryTest.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("stationid", stationid);
            example1.orderBy("datatime").asc();
            List<DlqjchistoryTest> list = dlqjchistorytestmapper.selectByExample(example1);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, String.format("%04x", record.getDeviceaddr() - 6010));
                devmap.put(index++, String.format("%04x", (short) (1.2 * 1000)));
                devmap.put(index++, String.format("%04x", (short) (1.001 * 1000)));
                devmap.put(index++, String.format("%04x", 0));
                devmap.put(index++, Converttime1(record.getDatatime()));
                devmap.put(index++, Converttime2(record.getDatatime()));
                devmap.put(index++, String.format("%04x", 0));
                byte[] bt;
                String chstr = "";
                try {
                    bt = record.getPointname().getBytes("gbk");
                    for (int j = 0; j < 32; j++) {
                        if (bt.length > j) {
                            chstr += String.format("%02x", bt[j]);
                        } else {
                            chstr += "00";
                        }
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                for (int j = 0; j < 64; j += 4) {
                    devmap.put(index++, chstr.substring(j, j + 4));
                }
                devmap.put(index++, "0002");
                if (record.getLaststate() == 3 || record.getLaststate() == 4) {
                    devmap.put(index++, String.format("%04x", (short) (3 * 100)));
                } else {
                    devmap.put(index++, String.format("%04x", (short) (1 * 100)));
                }
                devmap.put(index++, String.format("%04x", (short) (0.1 * 100)));
                devmap.put(index++, String.format("%04x", 0));
                devmap.put(index++, String.format("%04x", 0));
                devmap.put(index++, String.format("%04x", 100));

                String[] cmdtype = { "0001", "0002", "0003" };
                String[] statetype = { "0000", "0001", "0002", "0003", "0004" };
                devmap.put(index++, cmdtype[record.getCmd() - 64]);
                devmap.put(index++, statetype[record.getLaststate()]);
                devmap.put(index++, String.format("%08x", record.getId().intValue()).substring(0, 4));
                devmap.put(index++, String.format("%08x", record.getId().intValue()).substring(4, 8));
                float[] x = new float[6];
                float[] y = new float[6];
                x[0] = record.getT0time();
                x[1] = record.getT1time();
                x[2] = record.getT2time();
                x[3] = record.getT3time();
                x[4] = record.getT4time();
                x[5] = record.getT5time();
                y[0] = record.getI0current();
                y[1] = record.getI1current();
                y[2] = record.getI2current();
                y[3] = record.getI3current();
                y[4] = record.getI4current();
                y[5] = record.getI5current();
                float[][] xy = { { x[0], y[0] }, { x[1], y[1] }, { x[2], y[2] }, { x[3], y[3] }, { x[4], y[4] },
                        { x[5], y[5] } };
                float[][] res = bezier(xy, 100);
                for (int j = 0; j < 100; j++) {
                    Float f = res[j][1] * 10;
                    devmap.put(index++, String.format("%04x", f.shortValue()));
                }
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "dlqjc");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0085";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", appdevice.getAddrmin() - 6009);
                s += String.format("%04x", appdevice.getAddrmax() - 6009);
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%04x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveSf6(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Sf6sensor record = new Sf6sensor();
            record.setStationid(stationid);
            List<Sf6sensor> list = sf6sensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLasttemperature()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastpressure()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastdensity()) * 100)));
                if (isnull(record.getLastppm()) > 655.35) {
                    devmap.put(index++, "FFFF");
                } else {
                    devmap.put(index++,
                            String.format("%04x", (short) Math.round(isnull(record.getLastppm()) * 100) & 0xFFFF));
                }
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastchangya()) * 100)));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "sf6");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0009";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveZxdBlq(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Zxdblqsensor record = new Zxdblqsensor();
            record.setStationid(stationid);

            List<Zxdblqsensor> list = zxdblqsensorServiceImpl.getLists(record, groupstart, groupend);
            int index = 101;
            Random rd = new Random();
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);

                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", isnull(record.getDeviceaddr())));
                devmap.put(index++, String.format("%04x", isnull(record.getLastthundercount())));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastwcurrent()) * 1000)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastrcurrent()) * 1000)));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastwalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastwalarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastralarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastralarmstatus())));
                devmap.put(index++, Converttime1(record.getLastthundertime1()));
                devmap.put(index++, Converttime2(record.getLastthundertime1()));

                devmap.put(index++, String.format("%04x", convertStatus(record.getLastalarmlevel())));
                devmap.put(index++, String.format("%04x", convertStatus(record.getLastcommunicationstatus())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "zxdblq");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "000E";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size() + 101);
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    // private void resolveBlq(String str, int stationid, int groupstart, int
    // groupend,
    // OutputStream out) {
    // int startAddr = parseHex4(str.substring(4, 8));
    // int addrLength = parseHex4(str.substring(8, 12));
    // if (startAddr < 100) {
    // devmap.clear();
    // /*--------------- 这一段自定义代码 --------------- */
    // Gisblqsensor record = new Gisblqsensor();
    // record.setStationid(stationid);
    //
    // List<Gisblqsensor> list = gisblqsensorServiceImpl.getLists(record,
    // groupstart, groupend);
    // int index = 101;
    // Random rd = new Random();
    // for (int i = 0; i < list.size(); i++) {
    // record = list.get(i);

    // devmap.put(index++, Converttime1(record.getLastdatatime()));
    // devmap.put(index++, Converttime2(record.getLastdatatime()));
    // devmap.put(index++, String.format("%04x", isnull(record.getDeviceaddr())));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++,
    // String.format("%04x", (short) Math.round(isnull(record.getLastawcurrent()) *
    // 1000)));
    // devmap.put(index++,
    // String.format("%04x", (short) Math.round(isnull(record.getLastbwcurrent()) *
    // 1000)));
    // devmap.put(index++,
    // String.format("%04x", (short) Math.round(isnull(record.getLastcwcurrent()) *
    // 1000)));
    // devmap.put(index++,
    // String.format("%04x", (short) Math.round(isnull(record.getLastarcurrent()) *
    // 1000)));
    // devmap.put(index++,
    // String.format("%04x", (short) Math.round(isnull(record.getLastbrcurrent()) *
    // 1000)));
    // devmap.put(index++,
    // String.format("%04x", (short) Math.round(isnull(record.getLastcrcurrent()) *
    // 1000)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x",
    // isWarn(record.getLastwalarmstatus())));
    // devmap.put(index++, String.format("%04x", isWarn(0)));
    // devmap.put(index++, String.format("%04x", isWarn(0)));
    // devmap.put(index++, String.format("%04x",
    // isAlarm(record.getLastwalarmstatus())));
    // devmap.put(index++, String.format("%04x", isAlarm(0)));
    // devmap.put(index++, String.format("%04x", isAlarm(0)));
    // devmap.put(index++, String.format("%04x",
    // isWarn(record.getLastralarmstatus())));
    // devmap.put(index++, String.format("%04x", isWarn(0)));
    // devmap.put(index++, String.format("%04x", isWarn(0)));
    // devmap.put(index++, String.format("%04x",
    // isAlarm(record.getLastralarmstatus())));
    // devmap.put(index++, String.format("%04x", isAlarm(0)));
    // devmap.put(index++, String.format("%04x", isAlarm(0)));
    // devmap.put(index++, Converttime1(record.getLastthundertime1()));
    // devmap.put(index++, Converttime2(record.getLastthundertime1()));
    // devmap.put(index++, Converttime1(record.getLastthundertime2()));
    // devmap.put(index++, Converttime2(record.getLastthundertime2()));
    // devmap.put(index++, Converttime1(record.getLastthundertime3()));
    // devmap.put(index++, Converttime2(record.getLastthundertime3()));
    // devmap.put(index++, String.format("%04x",
    // isnull(record.getLastthundercount())));
    // devmap.put(index++, String.format("%04x",
    // isnull(record.getLastthundercount2())));
    // devmap.put(index++, String.format("%04x",
    // isnull(record.getLastthundercount3())));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() *
    // 1000)));
    // devmap.put(index++, String.format("%04x", Math.round(0 * 100)));
    // devmap.put(index++, String.format("%04x", Math.round(0 * 100)));
    // devmap.put(index++, String.format("%04x",
    // convertStatus(record.getLastalarmlevel())));
    // devmap.put(index++, String.format("%04x",
    // convertStatus(record.getLastcommunicationstatus())));
    // devmap.put(index++, String.format("%04x", isnull(0)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", convertStatus(0)));
    // devmap.put(index++, String.format("%04x", convertStatus(0)));
    // devmap.put(index++, String.format("%04x", isnull(0)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", (short) Math.round(isnull(0) *
    // 100)));
    // devmap.put(index++, String.format("%04x", convertStatus(0)));
    // devmap.put(index++, String.format("%04x", convertStatus(0)));

    // }
    // /*--------------- 这一段自定义代码 --------------- */
    // Example example = new Example(Appdevice.class);
    // Example.Criteria criteria = example.createCriteria();
    // criteria.andEqualTo("stationid", record.getStationid());
    // criteria.andEqualTo("devicegroup", record.getDevicegroup());
    // criteria.andEqualTo("simple", "gisblq");// 自定义
    // Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
    // if (appdevice != null) {
    // String s = "";
    // if (appdevice.getPointcount() != null) {
    // s += "0001";
    // s += String.format("%04x", appdevice.getPointcount());
    // } else {
    // s += "0000";
    // s += "0001";
    // }
    // s += "003F";// 自定义(单个设备的寄存器数量)
    // s += String.format("%04x", 1);
    // if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {//
    // 有监测点
    // s+=String.format("%04x",list.size()/appdevice.getPointcount());
    //
    // }else{s+=String.format("%04x",list.size());}

    // s += "0065";
    // s += String.format("%04x", devmap.size());
    // s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
    // s += CRC16.calcCrc16(s);
    // Send(s, out);
    // setText("Tx：" + s + "\r\n");
    // }
    // }
    // if (startAddr > 100) {
    // String head = str.substring(0, 4);
    // StringBuilder sb = new StringBuilder();
    // for (int i = startAddr; i < startAddr + addrLength; i++) {
    // if (i < (devmap.size() + startAddr)) {
    // sb.append(devmap.get(i));
    // } else {
    // sb.append("0000");
    // }
    // }
    // String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
    // s += CRC16.calcCrc16(s);
    // Send(s, out);
    // setText("Tx：" + s + "\r\n");
    // }
    // }
    private void resolveBlq(String str, int stationid, int groupstart, int groupend,
            OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Newblqsensor record = new Newblqsensor();
            record.setStationid(stationid);

            List<Newblqsensor> list = newblqsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            Random rd = new Random();
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);

                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", isnull(record.getDeviceaddr())));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastvref()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastacurrent()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastccurrent()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastarcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbrcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcrcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastaangle()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbangle()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcangle()) * 100)));
                devmap.put(index++, String.format("%04x",
                        isWarn(record.getLastawcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isWarn(record.getLastbwcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isWarn(record.getLastcwcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isAlarm(record.getLastawcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isAlarm(record.getLastbwcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isAlarm(record.getLastcwcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isWarn(record.getLastarcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isWarn(record.getLastbrcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isWarn(record.getLastcrcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isAlarm(record.getLastarcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isAlarm(record.getLastbrcurrentalarmstatus())));
                devmap.put(index++, String.format("%04x",
                        isAlarm(record.getLastcrcurrentalarmstatus())));
                devmap.put(index++, Converttime1(record.getLastthundertime()));
                devmap.put(index++, Converttime2(record.getLastthundertime()));
                devmap.put(index++, Converttime1(record.getLastthundertimeb()));
                devmap.put(index++, Converttime2(record.getLastthundertimeb()));
                devmap.put(index++, Converttime1(record.getLastthundertimec()));
                devmap.put(index++, Converttime2(record.getLastthundertimec()));
                devmap.put(index++, String.format("%04x",
                        isnull(record.getLastthundercount())));
                devmap.put(index++, String.format("%04x",
                        isnull(record.getLastthundercountb())));
                devmap.put(index++, String.format("%04x",
                        isnull(record.getLastthundercountc())));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastavacurrent()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastavbcurrent()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastavccurrent()) *
                                100)));
                devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() *
                        100)));
                devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() *
                        100)));
                devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() *
                        100)));
                devmap.put(index++, String.format("%04x",
                        convertStatus(record.getLastcommunicationstatusBlq())));
                devmap.put(index++, String.format("%04x",
                        convertStatus(record.getLastalarmlevelBlq())));
                devmap.put(index++, String.format("%04x", isnull(record.getPtaddr())));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getSimfrequence()) * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage()) * 100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage1()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage2()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage3()) *
                                100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage()) * 100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage1()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage2()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage3()) *
                                100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage()) * 100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage1()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage2()) *
                                100)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage3()) *
                                100)));
                devmap.put(index++, String.format("%04x",
                        convertStatus(record.getLastcommunicationstatusPt())));
                devmap.put(index++, String.format("%04x",
                        convertStatus(record.getLastalarmlevelPt())));
                devmap.put(index++, String.format("%04x", isnull(record.getWsdaddr())));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLasttemperature())
                                * 100)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasthumidity()) * 100)));
                devmap.put(index++, String.format("%04x",
                        convertStatus(record.getLastcommunicationstatusWsd())));
                devmap.put(index++, String.format("%04x",
                        convertStatus(record.getLastalarmlevelWsd())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "newblq");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "003F";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 0);
                s += String.format("%04x", list.size() - 1);
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    if (devmap.get(i) == null) {
                        sb.append("0000");
                    } else {
                        sb.append(devmap.get(i));
                    }
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private String Converttime1(Date lastdatatime) {
        if (lastdatatime == null) {
            return "0000";
        }
        try {
            return String.format("%08x", lastdatatime.getTime() / 1000).substring(0, 4);
        } catch (Exception e) {
            return "0000";
        }
    }

    private String Converttime2(Date lastdatatime) {
        if (lastdatatime == null) {
            return "0000";
        }
        try {
            return String.format("%08x", lastdatatime.getTime() / 1000).substring(4, 8);
        } catch (Exception e) {
            return "0000";
        }
    }

    private void resolveYggx(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            String simplename = "yggx";// 自定义(设备简称)
            String registercount = "0005";// 自定义(单个设备的寄存器数量)
            Yggxsensor record = new Yggxsensor();
            record.setStationid(stationid);
            List<Yggxsensor> list = yggxsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                Yggxsensor tmp = list.get(i);
                devmap.put(index++, Converttime1(tmp.getLastdatatime()));
                devmap.put(index++, Converttime2(tmp.getLastdatatime()));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmp.getLasttemper()) * 10)));
                devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastcommunicationstatus())));
                devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastalarmlevel())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", simplename);
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {// 有监测点
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {// 无监测点
                    s += "0000";
                    s += "0001";
                }
                s += registercount;
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveSctm(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            String simplename = "sctm";// 自定义(设备简称)
            String registercount = "0005";// 自定义(单个设备的寄存器数量)
            Sctmsensor record = new Sctmsensor();
            record.setStationid(stationid);

            List<Sctmsensor> list = sctmsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                Sctmsensor tmp = list.get(i);
                Float tmps[] = { tmp.getLastaupcontact(), tmp.getLastbupcontact(), tmp.getLastcupcontact(),
                        tmp.getLastalowcontact(), tmp.getLastblowcontact(), tmp.getLastclowcontact(),
                        tmp.getLastacable(), tmp.getLastbcable(), tmp.getLastccable() };
                for (int j = 0; j < 9; j++) {
                    devmap.put(index++, Converttime1(record.getLastdatatime()));
                    devmap.put(index++, Converttime2(record.getLastdatatime()));
                    devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmps[j]) * 10)));
                    devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastcommunicationstatus())));
                    devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastalarmlevel())));
                }
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", simplename);
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {// 有监测点
                    s += "0001";
                    s += String.format("%04x", 9);// 开关柜测温比较特殊
                } else {// 无监测点
                    s += "0000";
                    s += "0001";
                }
                s += registercount;
                s += String.format("%04x", 1);
                s += String.format("%04x", list.size() * appdevice.getPointcount());// 开关柜测温比较特殊
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            setText(s);
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveSctmn(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            String simplename = "sctmn";// 自定义(设备简称)
            String registercount = "0005";// 自定义(单个设备的寄存器数量)
            Sctmnsensor record = new Sctmnsensor();
            record.setStationid(stationid);

            List<Sctmnsensor> list = sctmnsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                Sctmnsensor tmp = list.get(i);
                Float tmps[] = { tmp.getLasta(), tmp.getLastb(), tmp.getLastc() };
                for (int j = 0; j < tmps.length; j++) {
                    devmap.put(index++, Converttime1(tmp.getLastdatatime()));
                    devmap.put(index++, Converttime2(tmp.getLastdatatime()));
                    devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmps[j]) * 10)));
                    devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastcommunicationstatus())));
                    devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastalarmlevel())));
                }
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", simplename);
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {// 有监测点
                    s += "0001";
                    s += String.format("%04x", 9);// 开关柜测温比较特殊
                } else {// 无监测点
                    s += "0000";
                    s += "0001";
                }
                s += registercount;
                s += String.format("%04x", 1);
                s += String.format("%04x", (int) Math.ceil(devmap.size() / 45.0));// 开关柜测温比较特殊
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    if (devmap.get(i) == null) {
                        sb.append("0000");
                    } else {
                        sb.append(devmap.get(i));
                    }
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            setText(s);
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveGispd(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Gispdsensor record = new Gispdsensor();
            record.setStationid(stationid);

            List<Gispdsensor> list = gispdsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                switch (isnull(record.getLastdischargetype())) {
                    case "颗粒放电":
                        devmap.put(index++, String.format("%04x", 0));
                        break;
                    case "间隙放电":
                        devmap.put(index++, String.format("%04x", 1));
                        break;
                    case "电晕放电":
                        devmap.put(index++, String.format("%04x", 2));
                        break;
                    case "电极放电":
                        devmap.put(index++, String.format("%04x", 3));
                        break;
                    case "空穴放电":
                        devmap.put(index++, String.format("%04x", 4));
                        break;
                    case "未知":
                        devmap.put(index++, String.format("%04x", 5));
                        break;
                    default:
                        devmap.put(index++, String.format("%04x", 0));
                        break;
                }
                devmap.put(index++, String.format("%04x", isnull(record.getLastrst())));
                devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(0, 4));
                devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(4, 8));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastdischargeamount()) * 100)));
                devmap.put(index++, String.format("%04x", isnull(record.getLastsignalpower())));
                devmap.put(index++, String.format("%04x", isnull(record.getLasthavesignal())));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "gispd");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "000B";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }

            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveTxpdIm(String str, int stationid, int groupstart, int groupend, OutputStream out) {// 010300650020540D
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Imsensor record = new Imsensor();
            record.setStationid(stationid);

            List<Imsensor> list = imsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            String txstr1 = "";
            String txstr2 = "";
            String txstr3 = "";
            String txstr4 = "";
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                if (record.getPointaddr() == 1) {
                    devmap.put(index++, Converttime1(record.getLastdatatime()));
                    devmap.put(index++, Converttime2(record.getLastdatatime()));
                    txstr1 = String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4);
                    txstr2 = String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8);
                    txstr3 = String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus())));
                    txstr4 = String.format("%04x", convertStatus(isnull(record.getLastalarmlevel())));
                }
                if (record.getPointaddr() == 2) {
                    devmap.put(index++, txstr1);
                    devmap.put(index++, txstr2);
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, txstr3);
                    devmap.put(index++, txstr4);
                    devmap.put(index++,
                            String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                }
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "im");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                s += "0000";
                s += "0001";
                s += "000A";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveTxpd(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Txpdsensor record = new Txpdsensor();
            record.setStationid(stationid);

            List<Txpdsensor> list = txpdsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            String txstr1 = "";
            String txstr2 = "";
            String txstr3 = "";
            String txstr4 = "";
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);

                if (record.getPointaddr() == 9) {
                    devmap.put(index++, Converttime1(record.getLastdatatime()));
                    devmap.put(index++, Converttime2(record.getLastdatatime()));
                    txstr1 = String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4);
                    txstr2 = String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8);
                    txstr3 = String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus())));
                    txstr4 = String.format("%04x", convertStatus(isnull(record.getLastalarmlevel())));
                }
                if (record.getPointaddr() == 10) {
                    devmap.put(index++, txstr1);
                    devmap.put(index++, txstr2);
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, txstr3);
                    devmap.put(index++, txstr4);
                    devmap.put(index++,
                            String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                }
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "txpd");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                s += "0000";
                s += "0001";
                s += "000A";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            setText("Tx：" + s + "\r\n");
            s += CRC16.calcCrc16(s);
            Send(s, out);
        }
    }

    public static void sortOilsensors(List<Oilsensor> list) {
        Collections.sort(list, new Comparator<Oilsensor>() {
            @Override
            public int compare(Oilsensor o1, Oilsensor o2) {
                return Integer.compare(o1.getDevicegroup(), o2.getDevicegroup());
            }
        });
    }

    private void resolveOilDGA3(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Oilfxsensor record = new Oilfxsensor();
            record.setStationid(stationid);

            List<Oilfxsensor> list = oilfxsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastch4()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h6()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h4()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasto2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastn2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasth2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastppm()) * 10)));
                devmap.put(index++, "0000");
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasttothyd()) * 10)));

                devmap.put(index++, String.format("%04x",isWarn(record.getLastch4alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLastch4alarmstatus())));
                devmap.put(index++, String.format("%04x",isWarn(record.getLastc2h6alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLastc2h6alarmstatus())));
                devmap.put(index++, String.format("%04x",isWarn(record.getLastc2h4alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLastc2h4alarmstatus())));
                devmap.put(index++, String.format("%04x",isWarn(record.getLastc2h2alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLastc2h2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastcoalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastcoalarmstatus())));
                devmap.put(index++, String.format("%04x",isWarn(record.getLastco2alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLastco2alarmstatus())));
                devmap.put(index++, String.format("%04x",isWarn(record.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x",isWarn(record.getLastn2alarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLastn2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLasth2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasth2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastppmalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastppmalarmstatus())));
                devmap.put(index++, "0000");
                devmap.put(index++, "0000");
                devmap.put(index++, String.format("%04x",isWarn(record.getLasttothydalarmstatus())));
                devmap.put(index++, String.format("%04x",isAlarm(record.getLasttothydalarmstatus())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "oilfx");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0027";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveOilDGA9(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Oilfxsensor record = new Oilfxsensor();
            record.setStationid(stationid);

            List<Oilfxsensor> list = oilfxsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastch4()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h6()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h4()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasto2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastn2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasth2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastppm()) * 10)));
                devmap.put(index++, "0000");
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasttothyd()) * 10)));

                devmap.put(index++, String.format("%04x", isWarn(record.getLastch4alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastch4alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastc2h6alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastc2h6alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastc2h4alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastc2h4alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastc2h2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastc2h2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastcoalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastcoalarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastco2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastco2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastn2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastn2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLasth2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasth2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastppmalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastppmalarmstatus())));
                devmap.put(index++, "0000");
                devmap.put(index++, "0000");
                devmap.put(index++, String.format("%04x", isWarn(record.getLasttothydalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasttothydalarmstatus())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "oilfx");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0027";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveOil(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Oilsensor record = new Oilsensor();
            record.setStationid(stationid);

            List<Oilsensor> list = oilsensorserviceimpl.getLists(record, groupstart, groupend);
            sortOilsensors(list);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastch4()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h6()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h4()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasto2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastn2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasth2()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastppm()) * 10)));
                devmap.put(index++, "0000");
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasttothyd()) * 10)));

                devmap.put(index++, String.format("%04x", isWarn(record.getLastch4alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastch4alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastc2h6alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastc2h6alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastc2h4alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastc2h4alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastc2h2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastc2h2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastcoalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastcoalarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastco2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastco2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastn2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastn2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLasth2alarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasth2alarmstatus())));
                devmap.put(index++, String.format("%04x", isWarn(record.getLastppmalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLastppmalarmstatus())));
                devmap.put(index++, "0000");
                devmap.put(index++, "0000");
                devmap.put(index++, String.format("%04x", isWarn(record.getLasttothydalarmstatus())));
                devmap.put(index++, String.format("%04x", isAlarm(record.getLasttothydalarmstatus())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "oil");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0027";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveByqpd(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Byqpdsensor record = new Byqpdsensor();
            record.setStationid(stationid);

            List<Byqpdsensor> list = byqpdsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                switch (isnull(record.getLastdischargetype())) {
                    case "颗粒放电":
                        devmap.put(index++, String.format("%04x", 0));
                        break;
                    case "间隙放电":
                        devmap.put(index++, String.format("%04x", 1));
                        break;
                    case "电晕放电":
                        devmap.put(index++, String.format("%04x", 2));
                        break;
                    case "电极放电":
                        devmap.put(index++, String.format("%04x", 3));
                        break;
                    case "空穴放电":
                        devmap.put(index++, String.format("%04x", 4));
                        break;
                    case "未知":
                        devmap.put(index++, String.format("%04x", 5));
                        break;
                    default:
                        devmap.put(index++, String.format("%04x", 0));
                        break;
                }
                devmap.put(index++, String.format("%04x", isnull(record.getLastrst())));
                devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(0, 4));
                devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(4, 8));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastdischargeamount()) * 100)));
                devmap.put(index++, String.format("%04x", isnull(record.getLastsignalpower())));
                devmap.put(index++, String.format("%04x", isnull(record.getLasthavesignal())));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "byqpd");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "000B";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveHjjk(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            String simplename = "o2andsf6";// 自定义(设备简称)
            String registercount = "000A";// 自定义(单个设备的寄存器数量)
            O2andsf6sensor record = new O2andsf6sensor();
            record.setStationid(stationid);

            List<O2andsf6sensor> list = o2andsf6sensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                O2andsf6sensor tmp = list.get(i);
                devmap.put(index++, Converttime1(tmp.getLastdatatime()));
                devmap.put(index++, Converttime2(tmp.getLastdatatime()));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmp.getLasttemperature()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmp.getLasthumidity()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmp.getLastsf6()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmp.getLasto2consistence()) * 10)));
                devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastsf6alarmstatus())));
                devmap.put(index++, String.format("%04x", convertStatus(tmp.getLasto2alarmstatus())));
                devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastcommunicationstatus())));
                devmap.put(index++, String.format("%04x", convertStatus(tmp.getLastalarmlevel())));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", simplename);
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {// 有监测点
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {// 无监测点
                    s += "0000";
                    s += "0001";
                }
                s += registercount;
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveGbzd(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Gbzdsensor record = new Gbzdsensor();
            record.setStationid(stationid);

            List<Gbzdsensor> list = gbzdsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastdata()) * 10)));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "gbzd");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0005";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveGbjf(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Gbjfsensor record = new Gbjfsensor();
            record.setStationid(stationid);

            List<Gbjfsensor> list = gbjfsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastelectricity()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastamplitude()) * 10)));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "gbjf");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0006";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveDlhl(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Dlhlsensor record = new Dlhlsensor();
            record.setStationid(stationid);

            List<Dlhlsensor> list = dlhlsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastacurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastbcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastccurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastdcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasta1current()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastb1current()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastc1current()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastd1current()) * 100)));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "dlhl");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "000C";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveDljf(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Dljfsensor record = new Dljfsensor();
            record.setStationid(stationid);

            List<Dljfsensor> list = dljfsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastadischarge()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastareq()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastaphase()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbdischarge()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbreq()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbphase()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcdischarge()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcreq()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcphase()) * 10)));

                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLasta1discharge()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasta1req()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasta1phase()) * 10)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastb1discharge()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastb1req()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastb1phase()) * 10)));
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastc1discharge()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc1req()) * 10)));
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc1phase()) * 10)));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "dljf");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0016";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveTg(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Tgsensor record = new Tgsensor();
            record.setStationid(stationid);

            List<Tgsensor> list = tgsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastawcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastbwcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcwcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttandeltaa()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttandeltab()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttandeltac()) * 100)));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastawalarmstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastbwalarmstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcwalarmstatus()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "tg");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "000E";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 0);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size() - 1);
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveXh(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Arcsupcoilsensor record = new Arcsupcoilsensor();
            record.setStationid(stationid);

            List<Arcsupcoilsensor> list = arcsupcoilsensorServiceImpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastvref()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcapacity()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcapcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttdegree()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastrescurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastgear()))));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcoilstatus()))));// 消弧线圈状态量
                // devmap.put(index++, String.format("%04x",
                // Math.round(isnull(record.getLastline()))));// 消弧线圈接地遥信
                // devmap.put(index++, String.format("%04x",
                // convertStatus(isnull(record.getLastcommunicationstatus()))));
                // devmap.put(index++, String.format("%04x",
                // convertStatus(isnull(record.getLastawalarmstatus()))));
                // devmap.put(index++, String.format("%04x",
                // convertStatus(isnull(record.getLastcommunicationstatus()))));
                // devmap.put(index++, String.format("%04x",
                // convertStatus(isnull(record.getLastbwalarmstatus()))));
                // devmap.put(index++, String.format("%04x",
                // convertStatus(isnull(record.getLastcommunicationstatus()))));
                // devmap.put(index++, String.format("%04x",
                // convertStatus(isnull(record.getLastcwalarmstatus()))));
            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "tg");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0006";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveDlqjxtx(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Dlqjxtxsensor record = new Dlqjxtxsensor();
            record.setStationid(stationid);

            List<Dlqjxtxsensor> list = dlqjxtxsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;
            for (int i = 0; i < list.size(); i++) {
                record = list.get(i);
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopenorclose()))));// 断路器分合状态
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLaststorage()))));// 断路器储能状态
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingcircuit()))));// 合闸回路状态
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningcircuit()))));// 分闸回路状态
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingcount()))));// 断路器合闸次数
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingtime()) * 0.1)));// 合闸时间
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingspeed()) * 0.1)));// 合闸速度
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingmaxcurrent()) * 0.1)));// 合闸线圈峰值(最大)电流
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingavecurrent()) * 0.1)));// 合闸线圈平均电流
                devmap.put(index++, Converttime1(record.getLastclosingdate()));// 合闸发生时间
                devmap.put(index++, Converttime2(record.getLastclosingdate()));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastclosingeffcurrent()) * 0.1)));// 合闸前一次有效电流
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningcount()))));// 断路器分闸次数
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningtime()) * 0.1)));// 分闸时间
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningspeed()) * 0.1)));// 分闸速度
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningmaxcurrent()) * 0.1)));// 分闸线圈峰值(最大)电流
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningavecurrent()) * 0.1)));// 分闸线圈平均电流
                devmap.put(index++, Converttime1(record.getLastopeningdate()));// 分闸发生时间
                devmap.put(index++, Converttime2(record.getLastopeningdate()));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastopeningeffcurrent()) * 0.1)));// 分闸前一次有效电流
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLaststoragecount()))));// 储能次数
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLaststoragetime()) * 0.1)));// 储能时间
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLaststoragemaxcurrent()) * 0.1)));// 储能电机峰值(最大)电流
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLaststorageavecurrent()) * 0.1)));// 储能电机平均电流
                devmap.put(index++, Converttime1(record.getLaststoragedate()));// 储能发生时间
                devmap.put(index++, Converttime2(record.getLaststoragedate()));
                devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastcommunicationstatus()))));

            }
            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "dlqjxtx");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                if (appdevice.getPointcount() != null) {
                    s += "0001";
                    s += String.format("%04x", appdevice.getPointcount());
                } else {
                    s += "0000";
                    s += "0001";
                }
                s += "0006";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (devmap.get(i) == null) {
                    sb.append("0000");
                } else {
                    sb.append(devmap.get(i));
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private void resolveYw(String str, int stationid, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        if (startAddr < 100) {
            devmap.clear();
            /*--------------- 这一段自定义代码 --------------- */
            Txpdsensor record = new Txpdsensor();
            record.setStationid(stationid);

            List<Txpdsensor> list = txpdsensorserviceimpl.getLists(record, groupstart, groupend);
            int index = 101;

            int talm = 0;
            int tcom = 0;
            for (int i = 0; i < list.size(); i++) {

                record = list.get(i);
                if (record.getPointaddr() == 1) {
                    devmap.put(index++, Converttime1(record.getLastdatatime()));
                    devmap.put(index++, Converttime2(record.getLastdatatime()));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                    if (record.getLastalarmlevel() != 0) {
                        talm = 1;
                    }
                    if (record.getLastcommunicationstatus() != 0) {
                        tcom = 1;
                    }
                }
                if (record.getPointaddr() == 2) {
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                    if (record.getLastalarmlevel() != 0) {
                        talm = 1;
                    }
                    if (record.getLastcommunicationstatus() != 0) {
                        tcom = 1;
                    }
                }
                if (record.getPointaddr() == 3) {
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                    if (record.getLastalarmlevel() != 0) {
                        talm = 1;
                    }
                    if (record.getLastcommunicationstatus() != 0) {
                        tcom = 1;
                    }
                }
                if (record.getPointaddr() == 4) {
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                    if (record.getLastalarmlevel() != 0) {
                        talm = 1;
                    }
                    if (record.getLastcommunicationstatus() != 0) {
                        tcom = 1;
                    }
                }
                if (record.getPointaddr() == 5) {
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                    devmap.put(index++,
                            String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(record.getLastalarmlevel()))));
                    if (record.getLastalarmlevel() != 0) {
                        talm = 1;
                    }
                    if (record.getLastcommunicationstatus() != 0) {
                        tcom = 1;
                    }
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(tcom))));
                    devmap.put(index++, String.format("%04x", convertStatus(isnull(talm))));
                }
            }

            /*--------------- 这一段自定义代码 --------------- */
            Example example = new Example(Appdevice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("simple", "txpd");// 自定义
            Appdevice appdevice = appdeviceserviceimpl.selectByExample(example).get(0);
            if (appdevice != null) {
                String s = "";
                s += "0000";
                s += "0001";
                s += "0013";// 自定义(单个设备的寄存器数量)
                s += String.format("%04x", 1);
                if (appdevice.getPointcount() != null && appdevice.getPointcount() > 0) {// 有监测点
                    s += String.format("%04x", list.size() / appdevice.getPointcount());
                } else {
                    s += String.format("%04x", list.size());
                }
                s += "0065";
                s += String.format("%04x", devmap.size());
                s = str.substring(0, 4) + String.format("%02x", s.length() / 2) + s;
                s += CRC16.calcCrc16(s);
                Send(s, out);
                setText("Tx：" + s + "\r\n");
            }
        }
        if (startAddr > 100) {
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s, out);
            setText("Tx：" + s + "\r\n");
        }
    }

    private Integer convertStatus(Integer status) {
        if (status == null || status == 0)
            return 0;
        else
            return 1;
    }

    private Integer isAlarm(Integer status) {
        if (status == null) {
            return 0;
        }
        if (status == 2)
            return 2;
        else
            return 0;
    }

    private Integer isWarn(Integer status) {
        if (status == null) {
            return 0;
        }
        if (status == 1)
            return 1;
        else
            return 0;
    }

    public void setText(String str) {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]:");
        String datestr = sdf.format(d);
        TestWebSocketHandler.sendAllMessage(datestr + str,
                ad.getSimple() + "s" + ad.getStationid() + "d" + ad.getDevicegroup());
    }
}
